/**
 * 
 */
package name.marz.to.go.btree.ui.swing;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;

import javax.swing.JPanel;

import name.marz.to.go.btree.node.BTree;


/**
 * @author masaru-gotou
 *
 */
public class TreePanel extends JPanel {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private TreeDrawer drawer;
	private Image d_buffer = null;
	private BTree<?> tree;
	
	public TreePanel(GraphicsConfigration gc) {
		super(false);	// ダブルバッファリングは自前で実装
		drawer = new TreeDrawer(gc);
		Dimension size = new Dimension(400,200);
		setSize(size);
		setPreferredSize(size);
	}
	
	public void setTree(BTree<?> tree) {
		this.tree = tree;
	}
	
	/* (non-Javadoc)
	 * @see javax.swing.JComponent#paintComponent(java.awt.Graphics)
	 */
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if ( tree != null ) {
			draw(tree,g);
		}
	}
	private void draw(BTree<?> tree,Graphics g) {
		d_buffer = createImage(getWidth(),getHeight());
		Graphics dg = d_buffer.getGraphics();
//		Dimension d = getSize();
		// 画面クリア
		dg.clearRect(0,0,getWidth(),getHeight());
		// ダブルバッファに描画
		drawer.draw(tree, dg, new Point(getWidth()/2,20));
		// パネルのサイズを変更
		setSize(drawer.getBounds());
		setPreferredSize(drawer.getBounds());
		// ウィンドウを更新する
		g.drawImage(d_buffer,0,0,this);
	}
}
